package com.dj.mingkong.admin.biz.controller.music;

import com.dj.mingkong.admin.biz.controller.music.vo.request.MusicianAddVo;
import com.dj.mingkong.admin.biz.controller.music.vo.request.MusicianQueryVo;
import com.dj.mingkong.admin.biz.controller.music.vo.request.MusicianUpdateVo;
import com.dj.mingkong.admin.biz.controller.music.vo.response.MusicianVo;
import com.dj.mingkong.admin.biz.service.music.MusicianService;
import com.dj.mingkong.common.annotation.HasRoles;
import com.dj.mingkong.common.enums.RoleEnum;
import com.dj.mingkong.common.infrastructure.base.dto.delete.DeleteVo;
import com.dj.mingkong.common.infrastructure.base.dto.page.PageVo;
import com.dj.mingkong.common.infrastructure.base.dto.rest.RestResponse;
import com.dj.mingkong.common.util.AssertUtils;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 音乐人管理
 *
 * @author zenglonghui
 * @date: 2022/7/22 15:40
 */
@RestController
@RequestMapping("/musician")
public class MusicianController {
    @Resource
    private MusicianService musicianService;

    /**
     * 新增数据
     *
     * @param addVo 实例对象
     * @return 是否成功，true-成功，false-失败
     * @throws RuntimeException musicianVo为空的时候会抛出运行时异常
     */
    @PostMapping("/add")
    @HasRoles(RoleEnum.ADMIN)
    public RestResponse<Boolean> insert(@RequestBody @Validated MusicianAddVo addVo) {
        musicianService.insert(addVo);
        return RestResponse.buildSuccess();
    }

    /**
     * 修改数据
     *
     * @param updateVo 实例对象
     * @return 是否成功，true-成功，false-失败
     * @throws RuntimeException musicianVo. getId()为空的时候会抛出运行时异常
     */
    @PostMapping("/updateById")
    public RestResponse<Boolean> updateById(@RequestBody @Validated MusicianUpdateVo updateVo) {
        musicianService.updateById(updateVo);
        return RestResponse.buildSuccess();
    }

    /**
     * 通过主键删除数据
     *
     * @param deleteVo -id 主键
     * @return 是否成功，true-成功，false-失败
     * @throws RuntimeException id 为空的时候会抛出运行时异常
     */
    @PostMapping("/deleteById")
    @HasRoles(RoleEnum.ADMIN)
    public RestResponse<Boolean> deleteById(@RequestBody @Validated DeleteVo deleteVo) {
        musicianService.deleteById(deleteVo);
        return RestResponse.buildSuccess();
    }

    /**
     * 查询多条数据
     *
     * @param queryVo 查询Vo queryVo - pageNum 查询第几页，如果输入是 < 1 那么会被重置成1 queryVo - pageSize
     *                查询分页大小，如果输入是 < 1 那么会被重置成10
     * @return 对象分页
     * @throws RuntimeException MusicianQueryVo为空的时候会抛出运行时异常
     */
    @PostMapping("/pageByVo")
    public RestResponse<PageVo<MusicianVo>> pageByVo(@RequestBody @Validated MusicianQueryVo queryVo) {
        AssertUtils.checkQueryPageVo(queryVo);
        return RestResponse.success(musicianService.pageByVo(queryVo));
    }

    /**
     * 获取所有音乐人列表 用于下拉框显示
     *
     * @return List<MusicianVo>
     */
    @GetMapping("/listAll")
    public RestResponse<List<MusicianVo>> listAll() {

        return RestResponse.success(musicianService.listAll());
    }

}
